<template>
  <div>
    <a-card :bordered="false" class="table-card">
      <!-- 增加 -->
      <biz-jinjian-bill-add-form
        v-if="showAddModal"
        ref="bizJinjianBillAddForm"
        :isZuofeiOptions="isZuofeiOptions"
        :caiwuCheckOptions="caiwuCheckOptions"
        :statusOptions="statusOptions"
        @ok="getList"
        @close="showAddModal = false"
      />
      <!-- 编辑 -->
      <biz-jinjian-bill-edit-form
        v-if="showEditModal"
        ref="bizJinjianBillEditForm"
        :isZuofeiOptions="isZuofeiOptions"
        :caiwuCheckOptions="caiwuCheckOptions"
        :statusOptions="statusOptions"
        @ok="getList"
        @close="showEditModal = false"
      />
      <biz-jinjian-bill-edit-zuofei-form
        v-if="showZuofeiModal"
        ref="bizJinjianBillEditZuofeiForm"
        :isZuofeiOptions="isZuofeiOptions"
        :caiwuCheckOptions="caiwuCheckOptions"
        :statusOptions="statusOptions"
        @ok="getList"
        @close="showZuofeiModal = false"
      />
      <advance-table
        title=" "
        :pagination="{
          current: queryParam.pageNum,
          pageSize: queryParam.pageSize,
          total: total,
          showSizeChanger: true,
          showLessItems: true,
          showQuickJumper: true,
          showTotal: (total, range) =>
            `第 ${range[0]}-${range[1]} 条，总计 ${total} 条`,
          onChange: changeSize,
          onShowSizeChange: onShowSizeChange,
        }"
        tableKey="base-bizJinjianBill-indexDetails-table"
        rowKey="id"
        size="middle"
        @refresh="getList"
        :columns="columns"
        :data-source="bizJinjianBillList"
        :loading="loading"
        :format-conditions="true"
        :scroll="{ x: '100%' }"
      >
        <span slot="isZuofei" slot-scope="{ record }">
          <a-tag color="green" v-if="record.isZuofei == 0">
            {{ isZuofeiFormat(record) }}
          </a-tag>
          <a-tag color="orange" v-if="record.isZuofei == 1">
            {{ isZuofeiFormat(record) }}
          </a-tag>
        </span>

        <span slot="caiwuCheck" slot-scope="{ record }">
          <a-tag color="orange" v-if="record.caiwuCheck == 0">
            {{ caiwuCheckFormat(record) }}
          </a-tag>
          <a-tag color="green" v-if="record.caiwuCheck == 1">
            {{ caiwuCheckFormat(record) }}
          </a-tag>
        </span>
        <span slot="jinjianId" slot-scope="{ text }">
          <a>{{ text }}</a>
        </span>

        <span slot="status" slot-scope="{ record }">
          <a-tag color="orange" v-if="record.status == 0">
            {{ statusFormat(record) }}
          </a-tag>
          <a-tag color="green" v-if="record.status == 1">
            {{ statusFormat(record) }}
          </a-tag>
        </span>
        <span slot="numStr" slot-scope="{ text }">
          ¥ {{ formatNumber(text) }}
        </span>

        <span slot="operation" slot-scope="{ text, record }">
          <a-button
            type="link"
            icon="money-collect"
            @click="handleAdd(record)"
            v-if="record.caiwuCheck == 0"
            v-hasPermi="['biz:bizJinjianBill:caiwuCheck']"
            >财务确认</a-button
          >
          <a-button
            type="link"
            icon="close"
            @click="handleZuofei(record)"
            v-if="record.caiwuCheck == 0"
            v-hasPermi="['biz:bizJinjianBill:zuofei']"
            >作废</a-button
          >
          <!-- <a-divider type="vertical" />
          <a @click="handleDelete(record)"> 删除 </a> -->
        </span>
        <span slot="rowindex" slot-scope="{ index }">
          {{ index + 1 }}
        </span>

        <span slot="serviceAmountCheck" slot-scope="{ text, record }">
          <a-input-number
            v-model="record.serviceAmountCheck"
            :min="0"
            :max="1000000000"
            placeholder="最小值"
            :formatter="(value) => `${value}元`"
            :parser="(value) => value.replace('元', '')"
            :precision="2"
            allow-clear
            v-if="record.caiwuCheck === '0'"
          />
          <span v-else>{{ record.serviceAmountCheck }}</span>
        </span>
      </advance-table>
    </a-card>
  </div>
</template>
<script>
import {
  listBizJinjianBill,
  delBizJinjianBill,
  exportBizJinjianBill,
  updateBizJinjianBill,
} from "@/api/biz/bizJinjianBill";
import AdvanceTable from "@/components/pt/table/AdvanceTable";
import BizJinjianBillAddForm from "@/views/biz/jinjianbill/modules/BizJinjianBillAddForm";
import BizJinjianBillEditForm from "@/views/biz/jinjianbill/modules/BizJinjianBillEditForm";
import BizJinjianBillEditZuofeiForm from "./modules/BizJinjianBillEditZuofeiForm";
export default {
  name: "BizJinjianBill",
  components: {
    AdvanceTable,
    BizJinjianBillAddForm,
    BizJinjianBillEditForm,
    BizJinjianBillEditZuofeiForm,
  },
  props: {
    jinjianId: {
      type: String,
    },
  },
  data() {
    return {
      showAddModal: false,
      showEditModal: false,
      showZuofeiModal: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 选中的主键集合
      selectedRowKeys: [],
      // 选中的数据集合
      selectedRows: [],
      // 高级搜索 展开/关闭
      advanced: false,
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 总条数
      total: 0,
      // label的百分比
      labelCol: { span: 6 },
      // 内容区域的百分比
      wrapperCol: { span: 18 },
      // 费用管理表格数据
      bizJinjianBillList: [],
      // 是否作废字典
      isZuofeiOptions: [],
      // 财务确认字典
      caiwuCheckOptions: [],
      // 结单状态字典
      statusOptions: [],
      // 查询参数
      queryParam: {
        pageNum: 1,
        pageSize: 10,
        userId: undefined,

        deptId: undefined,

        userName: undefined,

        deptName: undefined,

        jinjianId: this.jinjianId,

        caiwuCheck: undefined,

        cusName: undefined,

        status: undefined,
      },
      columns: [
        {
          title: "进件编号",
          dataIndex: "jinjianId",
          ellipsis: true,
          align: "left",
          // fixed: "left",
          width: "200px",
          scopedSlots: { customRender: "jinjianId" },
        },
        {
          title: "客户姓名",
          dataIndex: "cusName",
          ellipsis: true,
          align: "left",
          // fixed: "left",
          width: "120px",
        },

        {
          title: "放款金额(元)",
          dataIndex: "fkAmount",
          align: "right",
          width: "130px",
          scopedSlots: { customRender: "numStr" },
        },
        {
          title: "服务费(元)",
          dataIndex: "serviceAmount",
          align: "right",
          width: "130px",
          scopedSlots: { customRender: "numStr" },
        },
        {
          title: "回款金额(元)",
          dataIndex: "serviceConfirmAmount",
          align: "right",
          width: "130px",
          scopedSlots: { customRender: "numStr" },
        },

        {
          title: "确认收入金额(元)",
          dataIndex: "serviceAmountCheck",
          align: "right",
          width: "130px",
          scopedSlots: { customRender: "serviceAmountCheck" },
        },

        {
          title: "确认时间",
          dataIndex: "checkTime",
          align: "center",
          width: "110px",
        },
        {
          title: "是否作废",
          dataIndex: "isZuofei",
          scopedSlots: { customRender: "isZuofei" },
          align: "center",
          width: "80px",
        },
        {
          title: "确认状态",
          dataIndex: "caiwuCheck",
          scopedSlots: { customRender: "caiwuCheck" },
          align: "center",
          width: "80px",
        },
        {
          title: "备注信息",
          dataIndex: "remark",
          scopedSlots: { customRender: "remark" },
          align: "center",
          // width: "130px",
        },

        {
          title: "操作",
          dataIndex: "operation",
          align: "left",
          width: "180px",
          // fixed: "right",
          scopedSlots: { customRender: "operation" },
        },
      ],
    };
  },
  created() {
    this.getList();
    this.getDicts("is_zuofei_status").then((response) => {
      this.isZuofeiOptions = response.data;
    });
    this.getDicts("caiwu_check_status").then((response) => {
      this.caiwuCheckOptions = response.data;
    });
    this.getDicts("jiedan_status").then((response) => {
      this.statusOptions = response.data;
    });
  },
  methods: {
    formatNumber(num) {
      return parseFloat(num || 0).toFixed(2);
    },
    /** 查询费用管理列表 */
    getList() {
      this.loading = true;
      listBizJinjianBill(this.queryParam).then((response) => {
        this.bizJinjianBillList = response.data.list;
        this.total = response.data.total;
        this.loading = false;
      });
    },
    // 是否作废字典翻译
    isZuofeiFormat(row) {
      if (row.isZuofei) {
        return this.selectDictLabel(this.isZuofeiOptions, row.isZuofei);
      } else {
        return "";
      }
    },
    // 财务确认字典翻译
    caiwuCheckFormat(row) {
      if (row.caiwuCheck) {
        return this.selectDictLabel(this.caiwuCheckOptions, row.caiwuCheck);
      } else {
        return "";
      }
    },
    // 结单状态字典翻译
    statusFormat(row) {
      if (row.status) {
        return this.selectDictLabel(this.statusOptions, row.status);
      } else {
        return "";
      }
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParam.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.queryParam = {
        pageNum: 1,
        pageSize: 10,
        userId: undefined,

        deptId: undefined,

        userName: undefined,

        deptName: undefined,

        jinjianId: undefined,

        caiwuCheck: undefined,

        cusName: undefined,

        status: undefined,
      };
      this.handleQuery();
    },
    /** 翻页操作 */
    onShowSizeChange(current, pageSize) {
      this.queryParam.pageSize = pageSize;
      this.getList();
    },
    /** 翻页操作 */
    onSizeChange(current, size) {
      this.queryParam.pageNum = 1;
      this.queryParam.pageSize = size;
      this.getList();
    },
    /** 翻页操作 */
    changeSize(current, pageSize) {
      this.queryParam.pageNum = current;
      this.queryParam.pageSize = pageSize;
      this.getList();
    },
    /** 翻页操作 */
    onSelectChange(selectedRowKeys, selectedRows) {
      this.selectedRowKeys = selectedRowKeys;
      this.selectedRows = selectedRows;
      this.ids = this.selectedRows.map((item) => item.id);
      this.single = selectedRowKeys.length !== 1;
      this.multiple = !selectedRowKeys.length;
    },
    /** 查询折叠和展开操作 */
    toggleAdvanced() {
      this.advanced = !this.advanced;
    },
    handleAdd(record) {
      var that = this;
      this.$confirm({
        title: "确认金额无误?",
        onOk() {
          record.caiwuCheck = "1";
          const saveForm = JSON.parse(JSON.stringify(record));
          return updateBizJinjianBill(saveForm).then(() => {
            that.$message.success("确认成功", 3);
            that.open = false;
            that.getList();
            that.$emit("ok");
            that.$emit("close");
          });
        },
        onCancel() {},
      });
    },
    handleUpdate(record) {
      //   this.$message.warning("请勿重复结单", 3);
      //   return;
      // }
      this.showEditModal = true;
      this.$nextTick(() =>
        this.$refs.bizJinjianBillEditForm.handleUpdate(record)
      );
    },
    handleZuofei(record) {
      this.showZuofeiModal = true;
      this.$nextTick(() =>
        this.$refs.bizJinjianBillEditZuofeiForm.handleUpdateZuofei(record)
      );
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      var that = this;
      const bizJinjianBillIds = row.id || this.ids;
      this.$confirm({
        title: "确认删除所选中数据?",
        onOk() {
          return delBizJinjianBill(bizJinjianBillIds).then(() => {
            that.onSelectChange([], []);
            that.getList();
            that.$message.success("删除成功", 3);
          });
        },
        onCancel() {},
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      var that = this;
      this.$confirm({
        title: "是否确认导出?",
        content: "此操作将导出当前条件下所有数据而非选中数据",
        onOk() {
          return exportBizJinjianBill(that.queryParam).then((response) => {
            that.download(response.msg);
            that.$message.success("导出成功", 3);
          });
        },
        onCancel() {},
      });
    },
  },
};
</script>
